package cn.mediinfo.dianzidp.builder;

import cn.com.listentech.LEDControl;
import cn.com.listentech.ScreenInfo;
import cn.hutool.core.util.StrUtil;
import cn.mediinfo.dianzidp.dto.JianYanPinDto;
import cn.mediinfo.dianzidp.util.SendUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Slf4j
@Component
public class JianYanLEDBuilder{

    // 会话句柄 对于当前连接来说 每次只需要一个会话
    public int gi_handle = 0;
    public int ii_count = 0;

    public String ls_ip;

    //todo 传过去需要㐰号
    public int ls_port = 10000;

    //controlType 型号。2是T系列，3是E，Q系列卡
    public int gi_cotrolType = 3;

    public int ii_flag_2 = 0;

    /**
     * 检验屏幕推送
     *
     * @param mapInfo
     * @throws RuntimeException
     * @throws ParseException
     */
    public Boolean jy_ping_send(Map<String, JianYanPinDto> mapInfo) throws RuntimeException, ParseException {
        String ls_ip;
        String ls_value;
        String ls_value1;
        String ls_value2;
        String ls_value3;
        String ls_value4;
        String ls_value5;
        String ls_value6;
        String ls_value7;
        String ls_value8;
        Integer li_ret;
        Integer li_pno;
        Integer li_lenth_value1;
        Integer li_lenth_value2;
        Integer li_lenth_value3;
        Integer li_lenth_value4;
        Integer li_lenth_value5;
        Integer li_lenth_value6;
        Integer li_lenth_value7;
        Integer li_lenth_value8;
        String ls_space_value1;
        String ls_space_value2;
        String ls_space_value3;
        String ls_space_value4;
        String ls_space_value5;
        String ls_space_value6;
        String ls_space_value7;
        String ls_space_value8;
        String ls_head;
        Integer li_y;
        li_pno = 1;
        if (gi_handle > 0) {
            li_ret = LEDControl.EndSend(gi_handle);
        }
        //1.启动会话
        gi_handle = LEDControl.StartSend();
        //2.设置通讯模式
        if (LEDControl.SetTransMode(gi_handle, 1, 0, gi_cotrolType, 1) == 0) {
            throw new RuntimeException("通讯模式设置失败!");
        }
        ls_ip = "192.168.17.156";
        if (LEDControl.SetNetworkPara(gi_handle, li_pno, ls_ip, ls_port) == 2) {
            ii_count++;
            throw new RuntimeException("设置通信参数失败");
        }

        ScreenInfo sc = new ScreenInfo();
        sc.screenwidth = 2600;
        sc.screenheight = 160;
        sc.dbcolor = 1;
        sc.baud = 115200;
        sc.screenname = "";
        li_ret = LEDControl.SetScreenInfo(gi_handle, sc);
        li_ret = LEDControl.AddControl(gi_handle, li_pno, 2);
        li_ret = LEDControl.AddProgram(gi_handle, li_pno, 0);
        /**
         * 此处需要查询li_row 的相关数据
         */
        String chuangKou01 = "01";
        String chuangKou02 = "02";
        String chuangKou03 = "03";
        String chuangKou04 = "04";
        String chuangKou05 = "05";
        String chuangKou06 = "06";
        String chuangKou07 = "07";
        String chuangKou08 = "08";

        JianYanPinDto chuangkouxx_01 = mapInfo.get(chuangKou01);
        JianYanPinDto chuangkouxx_02 = mapInfo.get(chuangKou02);
        JianYanPinDto chuangkouxx_03 = mapInfo.get(chuangKou03);
        JianYanPinDto chuangkouxx_04 = mapInfo.get(chuangKou04);
        JianYanPinDto chuangkouxx_05 = mapInfo.get(chuangKou05);
        JianYanPinDto chuangkouxx_06 = mapInfo.get(chuangKou06);
        JianYanPinDto chuangkouxx_07 = mapInfo.get(chuangKou07);
        JianYanPinDto chuangkouxx_08 = mapInfo.get(chuangKou08);
        //窗口信息处理 -- 窗口空格数为10

        String chuangKouMC01 = getChuangKouMCByMC(chuangkouxx_01,10);
        String chuangKouMC02 = getChuangKouMCByMC(chuangkouxx_02,17);
        String chuangKouMC03 = getChuangKouMCByMC(chuangkouxx_03,19);
        String chuangKouMC04 = getChuangKouMCByMC(chuangkouxx_04,19);
        String chuangKouMC05 = getChuangKouMCByMC(chuangkouxx_05,21);
        String chuangKouMC06 = getChuangKouMCByMC(chuangkouxx_06,18);
        String chuangKouMC07 = getChuangKouMCByMC(chuangkouxx_07,17);
        String chuangKouMC08 = getChuangKouMCByMC(chuangkouxx_08,18);
        ls_head = StrUtil.builder().append(chuangKouMC01).append(chuangKouMC02).append(chuangKouMC03).append(chuangKouMC04)
                .append(chuangKouMC05).append(chuangKouMC06).append(chuangKouMC07).append(chuangKouMC08).toString();
        LEDControl.AddQuitText(gi_handle, 1, li_pno, 0, 5, 2600, 60, 255, "宋体", 20, true, false, false, ls_head);
        li_y = 48;
        // 这是五行数据，静态文本推送
        for (int li_i = 0; li_i < 2; li_i++) {
            if (li_i > 0) {
                li_y = li_y + 30;
            }
            ls_value1 = handleBingRenXM(chuangkouxx_01, li_i, 10);
            ls_value2 = handleBingRenXM(chuangkouxx_02, li_i, 10);
            ls_value3 = handleBingRenXM(chuangkouxx_03, li_i, 10);
            ls_value4 = handleBingRenXM(chuangkouxx_04, li_i, 10);
            ls_value5 = handleBingRenXM(chuangkouxx_05, li_i, 10);
            ls_value6 = handleBingRenXM(chuangkouxx_06, li_i, 10);
            ls_value7 = handleBingRenXM(chuangkouxx_07, li_i, 10);
            ls_value8 = handleBingRenXM(chuangkouxx_08, li_i, 10);

            li_lenth_value1 = 7;
            ls_space_value1 = SendUtil.getKongGeSpaces(li_lenth_value1);
            li_lenth_value2 = 10;
            ls_space_value2 = SendUtil.getKongGeSpaces(li_lenth_value2);
            li_lenth_value3 = 12;
            ls_space_value3 = SendUtil.getKongGeSpaces(li_lenth_value3);
            li_lenth_value4 = 12;
            ls_space_value4 = SendUtil.getKongGeSpaces(li_lenth_value4);
            li_lenth_value5 = 15;
            ls_space_value5 = SendUtil.getKongGeSpaces(li_lenth_value5);
            li_lenth_value6 = 12;
            ls_space_value6 = SendUtil.getKongGeSpaces(li_lenth_value6);
            li_lenth_value7 = 10;
            ls_space_value7 = SendUtil.getKongGeSpaces(li_lenth_value7);
            li_lenth_value8 = 11;
            ls_space_value8 = SendUtil.getKongGeSpaces(li_lenth_value8);
            ls_value = StrUtil.builder().append(ls_space_value1).append(ls_value1).append(ls_space_value2).append(ls_value2).
                    append(ls_space_value3).append(ls_value3).append(ls_space_value4).append(ls_value4).append(ls_space_value5).append(ls_value5).
                    append(ls_space_value6).append(ls_value6).append(ls_space_value7).append(ls_value7).append(ls_space_value8).append(ls_value8).toString();
            LEDControl.AddQuitText(gi_handle, 1, li_pno, 0, li_y, 2550, 30, 255, "新宋体", 18, false, false, false, ls_value);
        }
        String tixingStr = "                                                                           请抽血患者到窗口排队！                                                                 ";
        LEDControl.AddQuitText(gi_handle, 1, li_pno, 0, 114, 2550, 30, 255, "新宋体", 18, false, false, false, tixingStr);
        li_ret = LEDControl.SendControl(gi_handle, 1, 0);
        log.error("sendControl {}", li_ret);
        li_ret = LEDControl.EndSend(gi_handle);
        log.error("EndSend {}", li_ret);
        return true;
    }

    private String getChuangKouMCByMC(JianYanPinDto chuangKouXX,int chuangKouKGS) {
      String mc;
      if (chuangKouXX == null || StrUtil.isBlank(chuangKouXX.getChuangKouMC())){
          mc = SendUtil.getKongGeSpaces(chuangKouKGS);
      }else{
          mc = SendUtil.getKongGeSpaces(chuangKouKGS - SendUtil.getStrLength(chuangKouXX.getChuangKouMC())) + chuangKouXX.getChuangKouMC();
      }
      return mc;
    }

    /**
     * 药房LED处理病人姓名方法
     *
     * @return
     */
    private static String handleBingRenXM(JianYanPinDto chuangkouxx, int li, int jianGeChangDu) {
        String resValue = "";
        String bingRengXM = "";
        if (chuangkouxx == null || StrUtil.isBlank(chuangkouxx.getChuangKouMC())){
            return "";
        }
        if (li == 0){
            bingRengXM = Optional.ofNullable(chuangkouxx.getDangQianXH()).orElse("") + " " + Optional.ofNullable(chuangkouxx.getDangQianXM()).orElse("");
        }else{
            bingRengXM = Optional.ofNullable(chuangkouxx.getXiaYiWeiXH()).orElse("") + " " +Optional.ofNullable(chuangkouxx.getXiaYiWeiXM()).orElse("");
        }
        int totalChangDu = jianGeChangDu - SendUtil.getStrLength(bingRengXM);
        if (totalChangDu >= 0) {
            resValue = resValue + bingRengXM + SendUtil.getKongGeSpaces(totalChangDu);
        } else {
            resValue = resValue + SendUtil.getKongGeSpaces(jianGeChangDu);
        }
        return resValue;
    }
}
